package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id="2021000122667920";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key="MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQChEQPfONHBs1thprt5Sti5tlzpAbiDXUGTjtQ8otLDoKajKn2dCtikLUJBFaGySFRUK4Co4BtOfG3BducO5BkdbidBdQDkT5K2sH8TdMahqhKwA6zeU/h62LiawR+zUghUtzJiZJJj9DIiS2W8r9LKyhOCtxxaxfJEGFsDPuOos7u27rK/ZuKZ+TcYcb1Irf1EiNp1EGW+95Ngao8staj/0Yhb1Q4mhI7+xhg++2V2awcFSYclNa+Aj5ybjT3RpKIacr0z28Dr3IMjdOg3FsfIsOhBUL5erh6s70hH7OpuboazbwQDQdxYXyajLnX4D0Ggh5IZ1aNY6oyyKCVj13b1AgMBAAECggEBAJiT6MoWJkZLZN6tttzg9UuAIKWuNp9iwVNKM225hNVDI3hgMRyKgM2N/jxvhHzCHfiaOUhA/D0ujD6AJ8Qoek4x4kgU0aJ2avJbYhYJQn01AnzxDlqSy0l8cexP1Hrn0/KElNs2kKLVWoS4TgkfuhzWrwLd+aNX7vdo5xa9BabyE3kXxZ1qxbmVQ3enPX7f+XVi+TrFsRTY4CNoIJzaduZ3WU5AdeNezabbAh03k6oRmFMUcVR86/3sfCG/JEoOb1KxQoF4nR7nBSOrKrlA6t4vcMkcZQvIRoc/iD03J/igZ/otW89AFwLWtrtSxrwVROZ2loPJs1+0vPoEwdP8FyECgYEA0v6OAJn6Pivh9r1Nfg/5zfsAfGud08RLduS8CyJjFZPtOc1iwKPXT4oe99SVYlZNQf/o/7rW5TQhSBWGwSw0Y2JrbILMGU5olG9m1tW1jt2gZVQAIVSGYZHvhyFrKD4Ww1cmMDR5C9BYwQj5ez3qevXG92ChEBw8NiMti2zzhQ0CgYEAw2wf7dh6YZpaR2QdlS8S0WS0cOYeG+NIr2k0AZOcRsLzVGrnHPC30xhmHpza5MYUDZE6GJznIuSR8ucN+I6VPVU0ydmLmMn1FKYwwNWHOWAhNjcNAPGB9sdj9TxMScdwakUEq35oyxv2E+pagukrDVY8HsLJm0BgDfcPw5A/j4kCgYEAzIk9yTF7F8fcfh9Wvszn4ZjgCL0eO4p/iVsFO1nzaZiEEFOc1pg9gfqoOKFYywJUVJWrHjTWW0ykssdDYYHjqzBeaMuG5UVQNgykScDUBxGrim7azXSt7GnXJEzuHw3JbUXPXE2RxjULxN1ZU/JamyYk4exHVmG4sNZ7MTzF+QUCgYEAs9SH/UM8Oxzi/tcuMd5DntThBxN7mYYpmOFiriC7qpIzgPdu/b62KldFgd4twGBWi7WclJGuUPmtJK1iudvbkbka/rRmNY+DLCFSs+DKsw23AkDegoO7aPBLF50JRst0sxYYGggVPvLYG7OI3vbaW0eDw3rUMz2mJrO3merf0qECgYEAoxeu0Znf/QCFD9kHmmPFoM4O+A8T/UvqznIaDNQUQZoN6JUbG+HNnX+4+nL4YJJ1yPCwR943g1cC1shMva+LWJE6CIuDcxTm6SGNnSq9Ta9RbwLm+pklJ25QOzBGkL3e7ljTIoA3qO8KZ6/rEsoodvOROCIcChSKVgfAsLUjy4U=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgn0Ap5+cN60m5GH5R1n4PY+fzfefniMeP8bcfz8rgF4Qvohg1S+z8mPJnH3HKk0YyjH/+3skv/WmD78sCdl6i1HMCtVfNIYnGdIeAUkEpQlsTKhm+y9OFlWWCh9AvTdC0huYimpjwPLq4mF7x8+8cpHyfYkmDAfoCm2mYgvg0YG0PzDj8w1ZgTuElCPQR2SDs2kDskbH9piu3QY+rPMfb+0LPACKbyM3fkouJx5ZaFetSHiTET3EQBSDHHNeIZrdl70xAfPNjBP/1kTWVU01DUdFgJ1VgIWm4Cie2dUFijJlF7/JezupEZS6IA+vhF/J1pPDwwdb8MkjwNEqFQEvGwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url="";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url="http://member.gulimall.com/orderList.html";

    // 签名方式
    private  String sign_type="RSA2";

    // 字符编码格式
    private  String charset="utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl="https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
